<template>
  <div class="jeecg-basic-table jeecg-basic-table-form-container">
    <!--查询区域-->
    <div class="">
      <!-- <div class=""> -->
      <a-form ref="formRef" @keyup.enter.native="searchQuery" :model="queryParam" :label-col="labelCol"
        :wrapper-col="wrapperCol">
        <a-row :gutter="24">
          <a-col :lg="5">
            <a-form-item label="器具名称" name="uteName">
              <a-input v-model:value="queryParam.uteName"></a-input>
            </a-form-item>
          </a-col>
          <a-col :lg="5">
            <a-form-item label="器具自编号" name="uteNumber">
              <a-input v-model:value="queryParam.uteNumber"></a-input>
            </a-form-item>
          </a-col>

          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-col :lg="6">
                <a-button type="primary" preIcon="ant-design:search-outlined" @click="searchQuery">查询</a-button>
                <a-button type="primary" preIcon="ant-design:reload-outlined" @click="searchReset"
                  style="margin-left: 8px">重置</a-button>

              </a-col>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!--引用表格-->
    <BasicTable @register="registerTable" :columns="columnsHead" :rowSelection="rowSelection"
      @columns-change="handleColumnsChange" >
      <!-- 插槽:table标题 -->
      <template #tableTitle>
        <a-button type="primary" preIcon="ant-design:export-outlined" @click="onExportXls"> 导出</a-button>
        <a-button type="primary" preIcon="ant-design:export-outlined" @click="MyExport"> 自定义导出</a-button>
        <a-dropdown v-if="selectedRowKeys.length > 0">
          <template #overlay>
            <a-menu>
              <a-menu-item key="1" @click="batchHandleDelete">
                <Icon icon="ant-design:delete-outlined"></Icon>
                删除
              </a-menu-item>
            </a-menu>
          </template>
          <a-button>批量操作
            <Icon icon="mdi:chevron-down"></Icon>
          </a-button>
        </a-dropdown>
      </template>
      <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" />
      </template>
      <!--字段回显插槽-->
      <template #htmlSlot="{ text }">
        <div v-html="text"></div>
      </template>
      <!--省市区字段回显插槽-->
      <template #pcaSlot="{ text }">
        {{ getAreaTextByCode(text) }}
      </template>
      <template #fileSlot="{ text }">
        <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
        <a-button v-else :ghost="true" type="primary" preIcon="ant-design:download-outlined" size="small"
          @click="downloadFile(text)">下载</a-button>
      </template>
    </BasicTable>
    <!-- 表单区域 -->
    <LabUtensilModal ref="registerModal" @success="handleSuccess"></LabUtensilModal>
    <LabUtensilModalSure ref="registerModalSure" @success="handleSuccess"></LabUtensilModalSure>
    <LabUtensilModalNote ref="registerModalNote" @success="handleSuccess"></LabUtensilModalNote>
  </div>
</template>

<script lang="ts" name="utensil-labUtensil" setup>
import { ref, reactive, onMounted } from 'vue';
import { BasicTable, useTable, TableAction, GetColumnsParams, BasicColumn, TableSetting } from '/@/components/Table';
import { useListPage } from '/@/hooks/system/useListPage';
import { columns, columnsHead } from './LabUtensil.data';
import { list, deleteOne, batchDelete, getImportUrl, getExportUrl, saveOrUpdate } from './LabUtensil.api';
import { downloadFile } from '/@/utils/common/renderUtils';
import LabUtensilModal from './components/LabUtensilModal.vue';
import LabUtensilModalSure from './components/LabUtensilModalSure.vue';
import LabUtensilModalNote from './components/LabUtensilModalNote.vue';
import { getDataLogList } from '../../monitor/datalog/datalog.api';
import createTimePicker from 'ant-design-vue/lib/time-picker/time-picker';


const formRef = ref();
const queryParam = reactive<any>({});
const toggleSearchStatus = ref<boolean>(false);
const registerModal = ref();
const registerModalSure = ref();
const registerModalNote = ref();
//注册table数据
const { tableContext, onExportXls, onImportXls } = useListPage({
  tableProps: {
    title: '器具信息',
    api: list,
    // columns,
    canResize: false,
    useSearchForm: false,
    actionColumn: {
      width: 350,
      fixed: 'right',
    },
    showIndexColumn: true,
    tableSetting:
    {
      // 是否显示刷新按钮
      redo: true,
      // 是否显示尺寸调整按钮
      size: true,
      // 是否显示字段调整按钮
      setting: true,
      // 是否显示全屏按钮
      fullScreen: true,
    },
    beforeFetch: (params) => {
      return Object.assign(params, queryParam);
    },
  },
  exportConfig: {
    name: "器具信息",
    url: getExportUrl,
    params: queryParam,
  },
  importConfig: {
    url: getImportUrl,
    success: handleSuccess
  },
});
const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext;
const labelCol = reactive({
  xs: { span: 24 },
  sm: { span: 7 },
});
const wrapperCol = reactive({
  xs: { span: 24 },
  sm: { span: 16 },
});

/**自定义导出 */
const labUtensilData = ref([]);
let resultString = '';
function handleColumnsChange(newColumns) {
  resultString = '';
  labUtensilData.value = [];
  // 处理表格列的变化
  const visibleColumns = newColumns.filter(column => column.visible);
  visibleColumns.forEach(column => {
    labUtensilData.value.push(column.dataIndex);
  });
  for (let i = 0; i < labUtensilData.value.length; i++) {
    resultString += labUtensilData.value[i];
    // 添加逗号分隔符
    if (i !== labUtensilData.value.length - 1) {
      resultString += ',';
    }
  }
};

function MyExport() {
  //自定义的表头
  queryParam.myData = resultString;
  onExportXls();
}


/**
 * 编辑事件
 */
function handleEdit(record: Recordable) {
  registerModal.value.disableSubmit = false;

  registerModal.value.edit(record);

}
/**
 * 填写确认单
 * @param record 
 */
function sureForm(record: Recordable) {
  registerModalSure.value.disableSubmit = false;
  registerModalSure.value.edit(record);
}
/**
 * 查看确认单
 * @param record 
 */
function sureNote(record: Recordable) {
  registerModalNote.value.disableSubmit = false;
  registerModalNote.value.edit(record);
}

/**
 * 送检
 */
function submitYes(record) {
  record.inspDecide = '送检';
  record.status = '待检定';
  saveOrUpdate(record, true);
  record.submitY = true;
  record.submitN = true;
  record.submitE = true;
  record.submitF = true;
}
/**
 * 不送检
 */
function submitNo(record) {
  record.inspDecide = '不送检';
  saveOrUpdate(record, true);
  record.submitN = true;
}




/**
 * 成功回调
 */
function handleSuccess() {

  (selectedRowKeys.value = []) && reload();
}

function isRowChecked(record) {
  if (record.inspDecide === "已提交") {
    record.submitF = false; // 提交确认单按钮放开

    } else if (record.inspDecide === "送检") {
      record.submitF = true; // 提交确认单按钮禁用   
      record.submitY = true;
      record.submitN = true;
      record.submitE = true;
    } else if (record.inspDecide === "不送检") {
      record.submitF = true; // 提交确认单按钮禁用  
      record.submitN = true;
      record.submitE = false;
    } else {
      record.submitF = true; // 提交确认单按钮禁用  
      record.submitY = false;
      record.submitN = false;
      record.submitE = false;
    }
  return record;
}

/**
 * 操作栏
 */

function getTableAction(record) {
  return [
    {
      label: '编辑',
      onClick: handleEdit.bind(null, record),
      disabled:isRowChecked(record).submitE
    },
    {
      label: '送检',
      onClick: submitYes.bind(null, record),
      disabled:isRowChecked(record).submitY
    },
    {
      label: '不送检',
      onClick: submitNo.bind(null, record),
      disabled:isRowChecked(record).submitN
    },
    {
      label: '填写确认单',
      onClick: sureForm.bind(null, record),
      disabled:isRowChecked(record).submitF
    },
    {
      label: '查看确认单',
      onClick: sureNote.bind(null, record),
    },
  ];
}

/**
 * 查询
 */
function searchQuery() {
  reload();
}

/**
 * 重置
 */
function searchReset() {
  formRef.value.resetFields();
  selectedRowKeys.value = [];
  //刷新数据
  reload();
}



</script>

<style lang="less" scoped>
.jeecg-basic-table-form-container {
  .table-page-search-submitButtons {
    display: block;
    margin-bottom: 24px;
    white-space: nowrap;
  }

  .query-group-cust {
    width: calc(50% - 15px);
    min-width: 100px !important;
  }

  .query-group-split-cust {
    width: 30px;
    display: inline-block;
    text-align: center
  }
}

//修改的是查询位置整个板块的高度
.jeecg-basic-table-form {
  height: 58px;
}

:deep(.ant-popover-content) {
  width: 155px;
}
</style>
